function VehicleChartWindow(navGroup, vehicleId, chartType) {
	var self = Ti.UI.createWindow({
		title : 'Chart',
		barColor : '#3F4C6B',
		orientationModes: [
			Ti.UI.LANDSCAPE_LEFT,
			Ti.UI.LANDSCAPE_RIGHT
		]
	});

	var webview = Titanium.UI.createWebView({
		left : 0,
		top : 0,
		scalePagesToFit: true,
		disableBounce:true,
		url : 'vehiclechart.html'
	});
	
	self.add(webview);

	var todaysDate = new Date();
	var buttonBar = Titanium.UI.createButtonBar({
		labels : [todaysDate.getFullYear()],
		backgroundColor : 'black'
	});

	var flexSpace = Titanium.UI.createButton({
		systemButton : Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});

	self.setToolbar([flexSpace, buttonBar, flexSpace]);

	var orientationListener = Ti.Gesture.addEventListener('orientationchange', function(e) {
		if(e.orientation == Ti.UI.LANDSCAPE_LEFT) {
			Titanium.UI.orientation = Titanium.UI.LANDSCAPE_LEFT;
		}
		else if(e.orientation == Ti.UI.LANDSCAPE_RIGHT) {
			Titanium.UI.orientation = Titanium.UI.LANDSCAPE_RIGHT;
		}
	});
	
	self.addEventListener('open', function(e) {
		var VehicleConsumption = require('models/VehicleConsumption');

		var chartData = [];
		if(chartType == 'consumption') {
			chartData = getFuelConsumptionData(todaysDate, VehicleConsumption.findByVehicleID(vehicleId));
			if(!chartData) {
				Ti.UI.createAlertDialog({
					message : 'No fuel consumption data available',
					ok : 'Ok',
					title : 'Not enough fillups...'
				}).show();
				navGroup.close(self);
			} else {
				renderData = {
					'data' : chartData,
					'chartType': chartType
				};
				Ti.App.fireEvent('renderChart', renderData);
			}

		} else if(chartType == 'economy') {
			chartData = getFuelEconomyData(todaysDate, VehicleConsumption.findByVehicleID(vehicleId));
			if(!chartData) {
				Ti.UI.createAlertDialog({
					message : 'No fuel economy data available',
					ok : 'Ok',
					title : 'Not enough fillups...'
				}).show();
				navGroup.close(self);
			} else {
				renderData = {
					'data' : chartData,
					'chartType': chartType
				};
				Ti.App.fireEvent('renderChart', renderData);
			}
		}
	});
	
	self.addEventListener('close', function(e) {
		Titanium.UI.orientation = Titanium.UI.PORTRAIT;
	
		Ti.Gesture.removeEventListener(orientationListener);
	});
	
	return self;
}

function getFuelConsumptionData(todaysDate, consumptionRows) {
	// Convert consumption rows into fuel economy rows
	var economyRows = [];

	for(var i = 0; i < consumptionRows.length; i++) {
		
		var date = new Date(consumptionRows[i][0] * 1000);
		if(date.getFullYear() != todaysDate.getFullYear()) {
			continue;
		}

		var val = consumptionRows[i][1];
		economyRows.push([dayofyear(date), val]);
	}

	return economyRows;
}

function getFuelEconomyData(todaysDate, consumptionRows) {
	// Convert consumption rows into fuel economy rows
	var economyRows = [];
	var Conversions = require('tools/Conversions');

	for(var i = 0; i < consumptionRows.length; i++) {

		var date = new Date(consumptionRows[i][0] * 1000);
		if(date.getFullYear() != todaysDate.getFullYear()) {
			continue;
		}

		var val = Conversions.fillupUnitsPerDistanceToDistancePerFillupUnit(consumptionRows[i][1])*10;
		economyRows.push([dayofyear(date), val]);
	}

	return economyRows;
}

function dayofyear(d) {
	var onejan = new Date(d.getFullYear(), 0, 1);
	return Math.ceil((d - onejan) / 86400000);
}

module.exports = VehicleChartWindow;
